
//+============================================================================
// $Source: /disk/cvs//TANGO/DeviceServer/JClock/DSClock.java,v $
//
// project :	esrf
//
// Description:	java source code for the DSClock class and its commands.
//              This class is derived from DeviceImpl class.
//              It represents the CORBA servant obbject which
//              will be accessed from the network. All commands which
//              can be executed on the DSClock are implemented
//              in this file.
//
// $Author: raph $
//
// $Revision: 1.1.1.1 $
//
// $Log: DSClock.java,v $
// Revision 1.1.1.1  2001/04/17 11:21:05  raph
// initial work
//
//
// copyleft :   European Synchrotron Radiation Facility
//              BP 220, Grenoble 38043
//              FRANCE
//
//-============================================================================
//
//  		This file is generated by POGO
//	(Program Obviously used to Generate tango Object)
//
//             (c) - Pascal Verdier - ESRF
//=============================================================================


/**
 * @author	$Author: raph $
 * @version	$Revision: 1.1.1.1 $
 */


import java.util.*;
import java.text.*;
import org.omg.CORBA.*;
import Tango.*;
import TangoDs.*;



//===================================================================
//
//	The folowing table gives the correspondance
//	between commands and method's name.
//
//	Command's name	|	Method's name
//	----------------------------------------
//	DevState	|	dev_state()
//	DevStatus	|	dev_status()
//	getTimeDate	|	get_time_date()
//	setDateTime	|	set_date_time()
//
//===================================================================




/*
 *	Device States Description:
 *	Tango_RUNNING :	only in this state Device is ticking
 *	Tango_OFF :	
 */



/**
 *	Class Description:
 *	a clock device server
 *	
 *	RP LGM/ESRF
 */


public class DSClock extends DeviceImpl implements TangoConst
{
	protected	int	state;
	long offset=0;

	//	Start of attributes menmber data.
	protected short[]	attr_Seconde_read = new short[1];
	//	End of Attributes menmber data.

//=========================================================
/**
 *	Constructor for simulated Time Device Server.
 *
 *	@param	cl	The DeviceClass object
 *	@param	s	The Device name.
 */
//=========================================================
	DSClock(DeviceClass cl, String s) throws DevFailed
	{
		super(cl,s);
		init_device();
	}
//=========================================================
/**
 *	Constructor for simulated Time Device Server.
 *
 *	@param	cl	The DeviceClass object
 *	@param	s	The Device name.
 *	@param	d	Device description.
 */
//=========================================================
	DSClock(DeviceClass cl, String s, String d) throws DevFailed
	{
		super(cl,s,d);
		init_device();
	}


//=========================================================
/**
 *	Initialize the device.
 */
//=========================================================
	public void init_device()
	{
		System.out.println("DSClock() create " + dev_name);
		set_state(DevState.ON);
		offset=0;
		System.out.println("DSClock(" + dev_name + ") initialized");
	}

//===================================================================
/**
 *	Method called by the read_attributes CORBA operation to
 *	read device hardware
 *
 *	@param	attr_list	Vector of index in the attribute vector
 *		of attribute to be read
 */
//===================================================================			
	public void read_attr_hardware(Vector attr_list)
	{
		TangoUtil.out2.println("In read_attr_hardware for "+attr_list.size()+" attribute(s)");

		//	Switch on attribute name
		//---------------------------------
	}
//===================================================================
/**
 *	Method called by the read_attributes CORBA operation to
 *	set internal attribute value.
 *
 *	@param	attr	reference to the Attribute object
 */
//===================================================================			
	public void read_attr(Attribute attr) throws DevFailed
	{
		String attr_name = attr.get_name();
		TangoUtil.out2.println("In read_attr for attribute "+attr_name);

		//	Switch on attribute name
		//---------------------------------
		if (attr_name == "Seconde")
		{
			//	Add your own code here
		}
	}



//	Command Execution Methods

//=========================================================
/**
 *	Command to read the device state
 *
 *	@return the device state code.
 */
//=========================================================
	public DevState state_cmd()
	{

		TangoUtil.out2.println("In PowerSupply state command");
	
		return dev_state;
	}

//=========================================================
/**
 *	Command to read the device status
 *
 *	@return the device state as String.
 */
//=========================================================
	public String status_cmd()
	{

		TangoUtil.out2.println("In PowerSupply status command");
		return dev_status;
	}


//===============================================================
/**
 *	Execute command "getTimeDate" on device.
 *	return date and time of server
 *
 * @return	
 */
//===============================================================
	public String get_time_date() throws DevFailed
	{
		String	argout = new String();

		TangoUtil.out2.println("Entering get_time_date()");

		// ---Add your Own code to control device here ---
		argout = new Date(new Date().getTime()-offset).toString();
		
		TangoUtil.out2.println("Exiting get_time_date()");
		return argout;
	}


//===============================================================
/**
 *	Execute command "setDateTime" on device.
 *	set date and time of server
 *
 * @param	argin	
 */
//===============================================================
	public void set_date_time(String argin) throws DevFailed
	{
		TangoUtil.out2.println("Entering set_date_time()");

		// ---Add your Own code to control device here ---
		DateFormat df= DateFormat.getDateTimeInstance();
		System.out.println("date format = "+df.format(new Date()) +"\n"+
		argin);
		try {
			long d1 = df.parse(argin).getTime();
			long d2 = new Date().getTime();
			offset = d2 - d1;
			} catch(java.text.ParseException e)
			{System.out.println("date parsing error");}
		TangoUtil.out2.println("Exiting set_date_time()");
	}

//=========================================================
/**
 *	main part for the device server class
 */
//=========================================================
	public static void main(String[] argv)
	{
		try
		{
			TangoUtil tg = TangoUtil.init(argv,"dsclock");
			tg.server_init();

			System.out.println("Ready to accept request");

			tg.get_boa().impl_is_ready(null);
		}

		catch (OutOfMemoryError ex)
		{
			System.err.println("Can't allocate memory !!!!");
			System.err.println("Exiting");
		}
		catch (UserException ex)
		{
			TangoUtil.print_exception(ex);
			
			System.err.println("Received a CORBA user exception");
			System.err.println("Exiting");
		}
		catch (SystemException ex)
		{
			TangoUtil.print_exception(ex);
			
			System.err.println("Received a CORBA system exception");
			System.err.println("Exiting");
		}
		
		System.exit(-1);		
	}
}
	

//--------------------------------------------------------------------------
/* end of $Source: /disk/cvs//TANGO/DeviceServer/JClock/DSClock.java,v $ */
